package com.polaris.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.polaris.server.mapper.SysRoleMapper;
import com.polaris.server.mapper.SysUserMapper;
import com.polaris.server.mapper.SysUserRoleMapper;
import com.polaris.server.pojo.SysMenu;
import com.polaris.server.pojo.SysRole;
import com.polaris.server.pojo.SysUserRole;
import com.polaris.server.pojo.SysUser;
import com.polaris.server.service.ISysRoleMenuService;
import com.polaris.server.service.ISysUserRoleService;

import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author polaris
 * @since 2021-05-19
 */
@Service
public class ISysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    private final SysUserRoleMapper adminRoleRelationMapper;
    private final SysRoleMapper roleMapper;
    private final SysUserMapper userMapper;
    private final ISysRoleMenuService roleMenuService;
    public ISysUserRoleServiceImpl(SysUserRoleMapper adminRoleRelationMapper, SysRoleMapper roleMapper, SysUserMapper userMapper, ISysRoleMenuService roleMenuService) {
        this.adminRoleRelationMapper = adminRoleRelationMapper;
        this.roleMapper = roleMapper;
        this.userMapper = userMapper;
        this.roleMenuService = roleMenuService;
    }

    /**
     * 通过用户id查询对应role的具体实现
     * @param id
     * @return
     */
    @Override
    public SysRole getAdminRoleRelationByUserId(Long id) {
        QueryWrapper<SysUserRole> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("admin_id",id);
        SysUserRole adminRoleRelation = adminRoleRelationMapper.selectOne(objectQueryWrapper);
        return roleMapper.selectById(adminRoleRelation.getRoleId());
    }

    /**
     * 通过角色查询所有的用户
     *
     * @param id
     * @return
     */
    @Override
    public List<SysUser> getAdminRoleRelationByRoleId(Long id) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        SysUserRole adminRoleRelation = adminRoleRelationMapper.selectOne(queryWrapper);
        List<Long> idList = new ArrayList<>();
        idList.add(adminRoleRelation.getAdminId());
        return userMapper.selectBatchIds(idList);
    }
    /**
     * 通过userId获取对应的roleList
     * @param userId
     * @return
     */
    @Override
    public List<String> getRoleList(Long userId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        QueryWrapper<SysRole> queryWrapper1 = new QueryWrapper<>();
        SysRole sysRole;
        List<String> roleList = new ArrayList<>();
        queryWrapper.eq("admin_id", userId);
        List<SysUserRole> sysUserRoles = adminRoleRelationMapper.selectList(queryWrapper);
        for (SysUserRole sysUserRole : sysUserRoles) {
            queryWrapper1.clear();
             sysRole = roleMapper.selectOne(queryWrapper1.eq("role_id", sysUserRole.getRoleId()));
            if (sysRole!=null){
                roleList.add(sysRole.getCode());
            }
        }
        return roleList;
    }
    /**
     * 通过userId获取对应的roleIdList
     * @param userId
     * @return
     */
    @Override
    public List<Long> getRoleIdList(Long userId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        QueryWrapper<SysRole> queryWrapper1 = new QueryWrapper<>();
        SysRole sysRole;
        List<Long> roleList = new ArrayList<>();
        queryWrapper.eq("admin_id", userId);
        List<SysUserRole> sysUserRoles = adminRoleRelationMapper.selectList(queryWrapper);
        for (SysUserRole sysUserRole : sysUserRoles) {
            queryWrapper1.clear();
            sysRole = roleMapper.selectOne(queryWrapper1.eq("role_id", sysUserRole.getRoleId()));
            if (sysRole!=null){
                roleList.add(sysRole.getRoleId());
            }
        }
        return roleList;
    }

    /**
     * 通过userId获取menuList
     * @param userId
     * @return
     */
    @Override
    public List<SysMenu> getMenuList(Long userId) {
        List<Long> roleIdList = getRoleIdList(userId);
        return roleMenuService.getMenuList(roleIdList);
    }
}
